const http = require('http')
const https = require('https')
http.createServer((req, res) => {
  const urll = new URL(req.url, 'http://localhost:3000/')

  res.writeHead(200, {
    "Cpntent-Type": "application/json;charset=utf-8",
    "access-control-allow-origin": '*'
  })
  console.log(urll.pathname)
  switch (urll.pathname) {
    case '/data':
      httpPOST(dataStr => {
        res.end(dataStr)
      })
      break;

    default:
      res.end('404')
  }
}).listen(3000, () => console.log('创建成功！'))

function httpPOST(fn) {
  let dataStr = ''
  // https://m.xiaomiyoupin.com/mtop/market/search/placeHolder
  const obj = {
    hostname: 'm.xiaomiyoupin.com',
    port: '443',
    path: '/mtop/market/search/placeHolder',
    method: 'POST',
    headers: {
      "Content-Type": "application/json; charset=utf-8"
    }
  }
  const req = https.request(
    obj,
    res => {
      res.on('data', chunk => {
        dataStr += chunk
      })

      res.on('end', () => {
        console.log(dataStr)
        fn(dataStr)
      })
    })

  req.write(JSON.stringify([{}, { "baseParam": { "ypClient": 1 } }]))
  req.end()

}